﻿

'*******************************************************************
' Program: Data Center Visio Rack Tool
' Author: Albert E Edlund
' Date:
'
' Purpose:
'
'
'*******************************************************************

Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Visio
Imports Microsoft.Office.Interop.Excel


Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel

Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging


Imports visRackLinkAttributes
Imports visRackLinkAttributes.clsDC_RackData

Imports System.Data.OleDb
Imports System.IO
Imports Microsoft.Reporting.WinForms


Public Class frmLinkAttributes


#Region " Drawing Constants "



    ' the internal container stencil
    Const masterContainerStencilU As String = ""
    ' our default container
    Const masterContainerNameU As String = "Container 3"
    ' wireframe stencil for list (vertical w/header on top) control
    Const stencilWFControl As String = "wfctrl_u.vss"
    ' wireframe master for vertical list
    Const masterListBox As String = "List box"
    ' wireframe master for vertical list item
    Const masterListBoxItem As String = "List box item"
    ' wireframe stencil for list (vertical w/header on side and horizontal) toolbar 
    Const stencilWFToolbar As String = "wftlbr_u.vss"

    Const masterMenuBar As String = "Menu bar"
    Const masterMenuBarItem As String = "Menu bar item"

    Const masterDropDown As String = "Drop-down menu"
    Const masterDropDownItem As String = "Menu item"

    Const constDefaultContainerHeight As String = "1 in"
    Const constDefaultContainerWidth As String = "2 in"
    ' start .75 from left side of page and the top
    Const constDefaultContainerMargin As String = "0.75 in"


#End Region

#Region " Properties and definitions "


    ' our utilities for visio
    Private _clsVisio As clsVisioSupport

    ' our data source class
    Private _clsData As clsDC_RackData

    ' flag that we are testing with other than
    ' sql server data
    Private blnExcelSource As Boolean


    '********* visio snippet vb.net vis0041vb ******************
    Private _drawingApplication As Microsoft.Office.Interop.Visio.Application
    Public Property drawingApplication() As Microsoft.Office.Interop.Visio.Application
        Get
            Return Me._drawingApplication
        End Get
        Set(ByVal value As Microsoft.Office.Interop.Visio.Application)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("ApplicationValueIsNull", _
                    "drawingApplication is Null.")
            End If
            Me._drawingApplication = value
        End Set
    End Property

    '********* visio snippet vb.net vis0043vb ******************
    Private _drawingDocument As Microsoft.Office.Interop.Visio.Document
    Public Property drawingDocument() As Microsoft.Office.Interop.Visio.Document
        Get
            Return Me._drawingDocument
        End Get
        Set(ByVal value As Microsoft.Office.Interop.Visio.Document)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("DocumentValueIsNull", _
                    "drawingDocument is Null.")
            End If
            Me._drawingDocument = value
        End Set
    End Property

    '********* visio snippet vb.net vis0045vb ******************
    Private _drawingWindow As Microsoft.Office.Interop.Visio.Window
    Public Property drawingWindow() As Microsoft.Office.Interop.Visio.Window
        Get
            Return Me._drawingWindow
        End Get
        Set(ByVal value As Microsoft.Office.Interop.Visio.Window)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("WindowValueIsNull", _
                    "drawingWindow is Null.")
            End If
            Me._drawingWindow = value
        End Set
    End Property

    '********* visio snippet vb.net vis0044vb ******************
    Private _drawingPage As Microsoft.Office.Interop.Visio.Page
    Public Property drawingPage() As Microsoft.Office.Interop.Visio.Page
        Get
            Return Me._drawingPage
        End Get
        Set(ByVal value As Microsoft.Office.Interop.Visio.Page)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("PageValueIsNull", _
                    "drawingPage is Null.")
            End If
            Me._drawingPage = value
        End Set
    End Property

    '********* visio snippet vb.net vis0042vb ******************
    Private _drawingControl As AxMicrosoft.Office.Interop.VisOcx.AxDrawingControl
    Public Property drawingControl() As AxMicrosoft.Office.Interop.VisOcx.AxDrawingControl
        Get
            Return Me._drawingControl
        End Get
        Set(ByVal value As AxMicrosoft.Office.Interop.VisOcx.AxDrawingControl)
            Me._drawingControl = value
        End Set
    End Property

    Private m_clsReporting As visRackLinkAttributes.clsDC_RackReporting
    Private Property _clsReporting As visRackLinkAttributes.clsDC_RackReporting
        Get
            Return m_clsReporting
        End Get
        Set(ByVal value As visRackLinkAttributes.clsDC_RackReporting)
            m_clsReporting = value
        End Set
    End Property

    Private _CompName As String
    Public Property compName As String
        Get
            Return Me._CompName
        End Get
        Set(ByVal value As String)
            Me._CompName = value
            Me.lblLinkName.Text = _CompName
        End Set
    End Property

    Private _linkKey As Int32
    Public Property linkKey As Int32
        Get
            Return Me._linkKey
        End Get
        Set(ByVal value As Int32)
            Me._linkKey = value
            Me.lblLinkKey.Text = value.ToString
        End Set
    End Property

    Private _linkPortAKey As Int32
    Public Property linkPortAKey As Int32
        Get
            Return Me._linkPortAKey
        End Get
        Set(ByVal value As Int32)
            Me._linkPortAKey = value
        End Set
    End Property

    Private _linkPortBKey As Int32
    Public Property linkPortBKey As Int32
        Get
            Return Me._linkPortBKey
        End Get
        Set(ByVal value As Int32)
            Me._linkPortBKey = value
        End Set
    End Property



    Private _compKey As Int32
    Public Property compKey As Int32
        Get
            Return Me._compKey
        End Get
        Set(ByVal value As Int32)
            Me._compKey = value
        End Set
    End Property

    Private _portCompAKey As Int32
    Public Property portCompAKey As Int32
        Get
            Return Me._portCompAKey
        End Get
        Set(ByVal value As Int32)
            Me._portCompAKey = value
        End Set
    End Property

    Private _portCompBKey As Int32
    Public Property portCompBKey As Int32
        Get
            Return Me._portCompBKey
        End Get
        Set(ByVal value As Int32)
            Me._portCompBKey = value
        End Set
    End Property

    Private _subLocAKey As Int32
    Public Property subLocAKey As Int32
        Get
            Return Me._subLocAKey
        End Get
        Set(ByVal value As Int32)
            Me._subLocAKey = value
        End Set
    End Property

    Private _subLocBKey As Int32
    Public Property subLocBKey As Int32
        Get
            Return Me._subLocBKey
        End Get
        Set(ByVal value As Int32)
            Me._subLocBKey = value
        End Set
    End Property



    Public m_customstencilpath As String = My.Settings.path_custom_stencils
    Public Property _CustomStencilPath() As String
        Get
            Return Me.m_customstencilpath
        End Get
        Set(ByVal value As String)
            Me.m_customstencilpath = value
        End Set
    End Property



#End Region

#Region " Constructor "



    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        ' if called with no parameters handle as an excel test case
        Me.blnExcelSource = True
        initLinkAttributeForm(6814)


    End Sub


    '
    ' the decision is to allow this to be a standalone windows form that is not anchored to an existing
    ' data class library, or allow it to pull it's own data. For right now it will be standalone
    '
    Public Sub New(ByVal intLinkKey As Int32)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        Me.blnExcelSource = False
        initLinkAttributeForm(intLinkKey)


    End Sub

    '
    ' this is a discrete step
    '
    Public Sub InitAndDrawAttributeData()

        Try


            drawLinkAttributeData()


            ' now the report viewer control stuff (has to be fired after draw components because it requires the data to have been loaded)
            '
            attributesReportViewer.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local

            Dim dtTemp As System.Data.DataTable = New System.Data.DataTable
            ' get a copy of the component table
            Try
                dtTemp = _clsData.m_ds.Tables(DC_RackTables.tblNetworkLink.ToString).Copy
            Catch rdTemp As Exception
                Exit Sub
            End Try

            ' clear any existing reporting class
            _clsReporting = Nothing
            ' load the component report
            _clsReporting = New visRackLinkAttributes.clsDC_RackReporting _
                    (Me.attributesReportViewer, _
                    _clsReporting.baseReports.rptNetworkLinksAll, _
                    dtTemp)
            _clsReporting._LocalViewer.RefreshReport()


        Catch rpt As ReportViewerException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(rpt, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If

        Catch ex As Exception
            '********* visio snippet vb.net vis0059vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try


    End Sub


    Private Sub initLinkAttributeForm(ByVal intLinkKey As Int32)

        Try


            Me.sblStatusOne.Text = "Uninitialized"

            linkKey = intLinkKey

            drawingControl = attributesDrawControl

            ' get the environment data from the drawing control
            InitTheDrawingControl()

            ' this will use the mouse action as a menu inside the drawing
            AddHandler drawingWindow.MouseUp, AddressOf mouseUp_EventHandler


            _clsVisio = New clsVisioSupport()
            _clsVisio.drawingApplication = drawingApplication
            _clsVisio.drawingDocument = drawingDocument
            _clsVisio.drawingPage = drawingPage
            _clsVisio._CustomStencilPath = My.Settings.path_custom_stencils

            ' because we use network surrogates and they can have availability data
            ' include network layers on the page
            AddNetworkLayersToPage(drawingPage)
            ' change the page orientation
            Dim visPageSheet As Microsoft.Office.Interop.Visio.Shape = drawingPage.PageSheet
            visPageSheet.Cells("PageWidth").Formula = "11 in"
            visPageSheet.Cells("PageHeight").Formula = "8.5 in"
            drawingPage.AutoSize = False


            ' create our data cache
            If _clsData Is Nothing Then
                _clsData = New clsDC_RackData
            Else
                _clsData = Nothing
                _clsData = New clsDC_RackData
            End If


            Me.LoadToolStripMenuItem.Enabled = blnExcelSource

            _clsData.drawingApplication = drawingApplication
            ' make the imported datatables available as datarecordsets within the document for later use by the enduser
            _clsData.drawingDocument = drawingDocument
            ' add the ability to save datarecordsets in the document for this solution
            _clsData._BlnDR = True

            Me.Refresh()


        Catch rpt As ReportViewerException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(rpt, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If

        Catch ex As Exception
            '********* visio snippet vb.net vis0059vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try




    End Sub


    '********* visio snippet vb.net vis0049vb ******************
    ' Properties and Definitions
    Private Const defaultDocumentName As String = "linkAttributesDocument"
    Private Const defaultPageName As String = "linkAttributesPage"

    Private Sub InitTheDrawingControl()

        Try

            '
            ' get the pointers
            '
            ' make sure the control does not change the pagesizebehavior
            Me.drawingControl.PageSizingBehavior = VisPageSizingBehaviors.visNeverResizePages

            Me.drawingWindow = Me.drawingControl.Window
            Me.drawingWindow.Activate()
            Me.drawingApplication = Me.drawingControl.Window.Application

            Me.drawingDocument = Me.drawingControl.Document
            Dim docSheet As Visio.Shape = Me.drawingDocument.DocumentSheet
            docSheet.NameU = defaultDocumentName
            Me.drawingDocument.DiagramServicesEnabled = VisDiagramServices.visServiceVersion140

            Me.drawingPage = Me.drawingControl.Window.Page
            Me.drawingPage.NameU = defaultPageName
            drawingWindow.Page = drawingPage

            Me.drawingApplication.Application.Settings.EnableAutoConnect = False
            Me.drawingApplication.OnDataChangeDelay = -2

            Me.drawingWindow.ShowGrid = False
            Me.drawingWindow.ShowPageTabs = True
            Me.drawingWindow.ShowRulers = False
            Me.drawingWindow.ShowScrollBars = True


        Catch com As COMException
            ' add com exception handler here
            '********* visio snippet vb.net vis0056vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(com, "Com+ Policy")
            If (rethrow) Then
                Throw
            End If

        Catch ex As Exception
            ' add exception handler here
            '********* visio snippet vb.net vis0059vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub




#End Region

#Region " Visio Events "

    '
    ' 
    '
    Private Sub mouseUp_EventHandler(ByVal mouseButton As Integer, _
                ByVal buttonState As Integer, _
                ByVal clickLocationX As Double, _
                ByVal clickLocationY As Double, _
                ByRef cancelProcessing As Boolean)

        Const tolerance As Double = 0.0001

        Dim clickedShapes As Microsoft.Office.Interop.Visio.Selection = Nothing
        Dim message As New System.Text.StringBuilder()

        Try

            ' Check if the left mouse mouseButton caused this event
            ' to occur.
            If mouseButton = CInt(Microsoft.Office.Interop.Visio. _
                VisKeyButtonFlags.visMouseLeft) Then

                ' Get the list of shapes at the click location.
                clickedShapes = getMouseClickShapes( _
                    drawingWindow.PageAsObj, clickLocationX, _
                    clickLocationY, tolerance)

                ' Check if any shapes were found.
                If ((Not clickedShapes Is Nothing) And _
                    (clickedShapes.Count = 1)) Then

                    ' Show the name of shape.
                    message.Append(clickedShapes.Item(1).NameU & _
                        System.Environment.NewLine)

                    If clickedShapes.Item(1).CellExists("user.attrkey", False) = True Then
                        Dim strAttrCell As String = clickedShapes.Item(1).Cells("user.attrkey").ResultStr("")
                        ' parse out the user cell
                        ' first string is the record field name
                        ' second string is the record key
                        Dim instance As String = strAttrCell
                        Dim charSeparators() As Char = {"_"c}
                        Dim count As Integer = 2
                        Dim options As StringSplitOptions = StringSplitOptions.None
                        Dim returnValue As String()

                        returnValue = instance.Split(charSeparators, _
                                count, options)

                        'If (drawingWindow.Application.AlertResponse = 0) Then
                        '    System.Windows.Forms.MessageBox.Show(message.ToString())
                        'End If

                        ' based on the field name load the reportviewer
                        loadItemReport(returnValue(0))


                    End If ' test for attrkey cell

                End If
            End If

        Catch com As COMException
            '********* visio snippet vb.net vis0056vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(com, "Com+ Policy")
            If (rethrow) Then
                Throw
            End If

        Catch ex As Exception
            '********* visio snippet vb.net vis0059vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    ' find the shape that was acted on 
    '
    Private Shared Function getMouseClickShapes( _
                ByVal clickedPage As Microsoft.Office.Interop.Visio.Page, _
                ByVal clickedLocationX As Double, _
                ByVal clickedLocationY As Double, _
                ByVal tolerance As Double) _
                As Microsoft.Office.Interop.Visio.Selection

        Dim clickedShapes As Microsoft.Office.Interop.Visio.Selection = Nothing

        Try

            ' Use the SpatialSearch method of the page to get the list
            ' of shapes at the location.
            clickedShapes = clickedPage.SpatialSearch( _
                clickedLocationX, clickedLocationY, _
                CInt(Microsoft.Office.Interop.Visio. _
                VisSpatialRelationCodes.visSpatialContainedIn), _
                tolerance, _
                CInt(Microsoft.Office.Interop.Visio. _
                VisSpatialRelationFlags.visSpatialFrontToBack))


        Catch com As COMException
            '********* visio snippet vb.net vis0056vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(com, "Com+ Policy")
            If (rethrow) Then
                Throw
            End If

        Catch ex As Exception
            '********* visio snippet vb.net vis0059vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try


        Return clickedShapes

    End Function



#End Region

#Region " Form Events "

    '
    '
    '
    Private Sub frmLinkAttributes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.attributesReportViewer.RefreshReport()

    End Sub

    '
    '
    '
    Private Sub ExitToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    '
    ' pass in a selected shape's name and prefix and load up an item report
    ' the name prefix is the field name for the key 
    '
    Private Sub loadItemReport(ByVal strNode As String)


        Dim dtWork As System.Data.DataTable = Nothing

        Try


            Dim dtWorkName As String = ""

            _clsReporting._LocalViewer = Me.attributesReportViewer
            _clsReporting._LocalViewer.Reset()
            _clsReporting._LocalViewer.LocalReport.DataSources.Clear()
            _clsReporting._DataSource = "Uninitialized"

            Select Case strNode

                Case "compKey"
                    ' component node
                    _clsReporting.initReports(_clsReporting.baseReports.rptComponentsAll)
                    dtWork = _clsData.m_ds.Tables(DC_RackTables.tblComponents.ToString).Copy
                    dtWorkName = DC_RackTables.tblComponents.ToString
                    Exit Select

                Case "attrlocKey"
                    ' component root sublocation data
                    _clsReporting.initReports(_clsReporting.baseReports.rptAttrLocationsAll)
                    dtWork = _clsData.m_ds.Tables(DC_RackTables.tblAttrLocation.ToString).Copy
                    dtWorkName = DC_RackTables.tblAttrLocation.ToString
                    Exit Select

                Case ("linkKey")
                    ' network link
                    _clsReporting.initReports(_clsReporting.baseReports.rptNetworkLinksAll)
                    dtWork = _clsData.m_ds.Tables(DC_RackTables.tblNetworkLink.ToString).Copy
                    dtWorkName = DC_RackTables.tblNetworkLink.ToString
                    Exit Select

                Case "attrnetKey"
                    ' component network
                    _clsReporting.initReports(_clsReporting.baseReports.rptAttrNetworkAll)
                    dtWork = _clsData.m_ds.Tables(DC_RackTables.tblAttrNetwork.ToString).Copy
                    dtWorkName = DC_RackTables.tblAttrNetwork.ToString
                    Exit Select

                Case "portKey"
                    ' component port data
                    _clsReporting.initReports(_clsReporting.baseReports.rptPortDataAll)
                    dtWork = _clsData.m_ds.Tables(DC_RackTables.tblAttrPortData.ToString).Copy
                    dtWorkName = DC_RackTables.tblAttrPortData.ToString
                    Exit Select

                Case Else

                    Exit Sub

            End Select


            ' let the system catch up
            System.Windows.Forms.Application.DoEvents()

            If dtWork Is Nothing Then
                MsgBox(strNode & " dtWork is nothing")
                Exit Sub
            End If
            If dtWork.Rows.Count < 1 Then
                MsgBox(strNode & " dtWork is empty")
                Exit Sub
            End If


            _clsReporting._DataTable = dtWork.Copy
            _clsReporting._DataTable.TableName = dtWorkName
            _clsReporting.createLocalReport()
            _clsReporting._LocalViewer.Refresh()
            _clsReporting._LocalViewer.RefreshReport()


        Catch sql As SqlException
            '********* visio snippet vb.net vis0057vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            '********* visio snippet vb.net vis0059vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        dtWork = Nothing


    End Sub

    '
    ' load a visio document
    '
    Private Sub LoadToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoadToolStripMenuItem.Click

        openVisioDocumentDialog()

    End Sub

    '
    ' save the visio document
    '
    Private Sub SaveToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click

        saveVisioDocumentDialog()

    End Sub

    '
    ' draw link attributes
    '
    Private Sub DrawDataToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs)


        drawLinkAttributeData()

        Me.sblStatusOne.Text = "Draw begin"
        Me.sblStatusTwo.Text = ""

        ' make sure the reporting class has been built in case the user
        ' selects a shape
        If _clsReporting Is Nothing Then
            Dim dtTemp As System.Data.DataTable = _clsData.m_ds.Tables(DC_RackTables.tblNetworkLink.ToString).Copy
            ' load the component report
            _clsReporting = New visRackLinkAttributes.clsDC_RackReporting _
                    (Me.attributesReportViewer, _
                    _clsReporting.baseReports.rptNetworkLinksAll, _
                    dtTemp)
            _clsReporting._LocalViewer.RefreshReport()
        End If



    End Sub

    '
    '
    '
    Private Sub LoadToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadToolStripMenuItem1.Click

        If blnExcelSource = True Then
            loadTestDictTables()
            loadExcelTestData()
        Else

        End If



    End Sub

    '
    '
    '
    Private Sub DrawToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DrawToolStripMenuItem.Click

        drawLinkAttributeData()


        'Dim dlgSummary As dlgDataSummary = New dlgDataSummary()
        'dlgSummary.lblReadTime.Text = ""
        'dlgSummary.ListDataTables(_clsData.m_ds)
        'dlgSummary.ShowDialog()
        'dlgSummary = Nothing

        Me.sblStatusOne.Text = "Draw Complete"
        Me.sblStatusTwo.Text = ""

        ' make sure the reporting class has been built in case the user
        ' selects a shape
        If _clsReporting Is Nothing Then
            Dim dtTemp As System.Data.DataTable = _clsData.m_ds.Tables(DC_RackTables.tblNetworkLink.ToString).Copy()
            ' load the component report
            _clsReporting = New visRackLinkAttributes.clsDC_RackReporting _
                    (Me.attributesReportViewer, _
                    _clsReporting.baseReports.rptNetworkLinksAll, _
                    dtTemp)
            _clsReporting._LocalViewer.RefreshReport()
        End If


    End Sub

    '
    '
    '
    Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click


        Dim frmAbout As frmAboutBox = New frmAboutBox
        frmAbout.ShowDialog()
        frmAbout.Dispose()
        frmAbout = Nothing

    End Sub

#End Region

#Region " draw attribute methods "



    '
    ' go through the data and add containers/lists to the drawing page
    '
    Private Sub drawLinkAttributeData()

        ' load our test data
        Me.Cursor = Cursors.WaitCursor
        _clsVisio.drawingPage = drawingPage

        Try

            Me.sblStatusOne.Text = "AttrDrawing"

            _clsVisio.drawingPage = drawingPage

            ' load the datagraphics information
            loadDGM()

            buildLinkContainer()
            Dim shpLink As Microsoft.Office.Interop.Visio.Shape = drawingPage.Shapes("contLinkData")
            _clsVisio.SetCellValueToString(shpLink.Cells("pinX"), "5.5 in")
            _clsVisio.SetCellValueToString(shpLink.Cells("pinY"), "6.425 in")

            buildPortContainers()
            Dim shpPortA As Microsoft.Office.Interop.Visio.Shape = drawingPage.Shapes("contPortDataA")
            _clsVisio.SetCellValueToString(shpPortA.Cells("pinX"), "3.5 in")
            _clsVisio.SetCellValueToString(shpPortA.Cells("pinY"), "5.625 in")

            Dim shpPortB As Microsoft.Office.Interop.Visio.Shape = drawingPage.Shapes("contPortDataB")
            _clsVisio.SetCellValueToString(shpPortB.Cells("pinX"), "7.5 in")
            _clsVisio.SetCellValueToString(shpPortB.Cells("pinY"), "5.625 in")

            ' load the components
            buildComponentContainers()
            Dim shpCompA As Microsoft.Office.Interop.Visio.Shape = drawingPage.Shapes("contCompDataA")
            _clsVisio.SetCellValueToString(shpCompA.Cells("pinX"), "1.5 in")
            _clsVisio.SetCellValueToString(shpCompA.Cells("pinY"), "5.625 in")
            Dim shpCompB As Microsoft.Office.Interop.Visio.Shape = drawingPage.Shapes("contCompDataB")
            _clsVisio.SetCellValueToString(shpCompB.Cells("pinX"), "9.5 in")
            _clsVisio.SetCellValueToString(shpCompB.Cells("pinY"), "5.625 in")


            ' paste the legend last
            Dim shpLegend As Microsoft.Office.Interop.Visio.Shape = PutOnLegend()
            _clsVisio.SetCellValueToString(shpLegend.Cells("pinX"), "9.5 in")
            _clsVisio.SetCellValueToString(shpLegend.Cells("pinY"), "3.0 in")

        Catch com As COMException
            '********* visio snippet vb.net vis0056vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(com, "Com+ Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            '********* visio snippet vb.net vis0059vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        Me.Cursor = Cursors.Default
        drawingWindow.DeselectAll()

    End Sub

    '
    ' load the datagraphics masters table and preload the document with the shapes and
    ' predefined datagraphics
    '
    Private Sub loadDGM()

        ' be consistent in how we apply the data graphics
        Dim _clsDGM As visRackLinkAttributes.clsDgMasters = New visRackLinkAttributes.clsDgMasters

        Try

            Me.sblStatusTwo.Text = "DGMs Started"

            _clsDGM.drawingApplication = drawingApplication
            _clsDGM.drawingDocument = drawingDocument
            _clsDGM._StencilPath = _CustomStencilPath

            ' load the table
            If blnExcelSource = False Then
                _clsDGM.dbSelectAllDGMasters()
                _clsDGM.preLoadDocumentStencil()
                ' finally save the datagraphics master data to the working dataset
                _clsData.loadDgMasterTable(_clsDGM.m_ds.Tables(DC_RackTables.tblDGMasters.ToString).Copy)
            Else
                _clsDGM.m_dt = _clsData.m_ds.Tables(DC_RackTables.tblDGMasters.ToString).Copy
                _clsDGM.preLoadDocumentStencil()
            End If

            Me.sblStatusTwo.Text = "DGMs Loaded"

        Catch com As COMException
            '********* visio snippet vb.net vis0056vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(com, "Com+ Policy")
            If (rethrow) Then
                Throw
            End If
        Catch sql As SqlException
            '********* visio snippet vb.net vis0057vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            '********* visio snippet vb.net vis0059vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If

        Finally

            _clsDGM = Nothing

        End Try

    End Sub


#Region " link Container "

    '***********************************************************************************
    ' link container
    '***********************************************************************************
    '
    ' this simply pulls up the record for the passed in link key and creates a container
    ' for it. The container holds a list  oriented horizontal control with a single surrogate.
    '
    Private Sub buildLinkContainer()

        Try

            Dim shpGroup As Microsoft.Office.Interop.Visio.Shape = Nothing

            ' link data
            shpGroup = loadLink(Me.linkKey)

            If shpGroup IsNot Nothing Then

                shpGroup.Name = "contLinkData"
                shpGroup.NameU = "contLinkData"


                Dim linkQuery = From link In _clsData.m_ds.Tables(DC_RackTables.tblNetworkLink.ToString).AsEnumerable() _
                    Where link!linkKey = Me.linkKey
                    Select link

                Dim drTemp As System.Data.DataRow = Nothing
                ' we should only have transferred in a single component key
                If linkQuery.Count = 1 Then
                    For Each l In linkQuery
                        Me.lblLinkName.Text = "linkKey_" & l.Item("linkKey")
                        Me.linkPortAKey = l.Item("linkPortAKey")
                        Me.linkPortBKey = l.Item("linkPortBKey")
                        Me.lblLinkLabel.Text = l.Item("linkCableLabel")
                    Next l
                End If

            End If



        Catch com As COMException
            '********* visio snippet vb.net vis0056vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(com, "Com+ Policy")
            If (rethrow) Then
                Throw
            End If

        Catch ex As Exception
            '********* visio snippet vb.net vis0059vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try
    End Sub

    '
    ' based on the Link key, read the link record from the data source
    '
    Private Function loadLink(ByVal intLinkKey As Int32) As Microsoft.Office.Interop.Visio.Shape

        Dim _clsLink As visRackLinkAttributes.clsNetworkLinks = New visRackLinkAttributes.clsNetworkLinks(drawingApplication, drawingDocument)
        Dim shpContainer As Microsoft.Office.Interop.Visio.Shape = Nothing

        Try

            Me.sblStatusTwo.Text = "Link Loading"

            _clsLink.drawingPage = drawingPage
            _clsLink._CustomStencilPath = _CustomStencilPath

            If blnExcelSource = False Then

                _clsData.loadNetworkLinksTable(-1, Nothing, _clsLink.dbSelectNetworkLinkByLinkKey(intLinkKey))

            End If

            ' returns a single link surrogate encased in a container/list shape
            shpContainer = _clsLink.buildLinkContainer(intLinkKey, _clsData)


            Me.sblStatusTwo.Text = "Link Loaded"


        Catch com As COMException
            '********* visio snippet vb.net vis0056vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(com, "Com+ Policy")
            If (rethrow) Then
                Throw
            End If
        Catch sql As SqlException
            '********* visio snippet vb.net vis0057vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            '********* visio snippet vb.net vis0059vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If

        Finally

            _clsLink = Nothing

        End Try

        Return shpContainer

    End Function


#End Region


#Region " Port Containers "


    '***********************************************************************************
    ' port container
    '***********************************************************************************
    '
    ' their needs to be two port containers (a-side and b-side). The port containers will hold
    ' two surrogate shapes, a port shape and an optional attrnet shape if network addressing has been
    ' applied to the port. These shapes are stored in a vertical oriented list control
    '
    Private Sub buildPortContainers()


        Dim _clsLinkPort As visRackLinkAttributes.clsAttrPortData = New visRackLinkAttributes.clsAttrPortData(drawingApplication, drawingDocument)
        Dim _clsPortNet As visRackLinkAttributes.clsAttrNetwork = New visRackLinkAttributes.clsAttrNetwork(drawingApplication, drawingDocument)
        _clsPortNet.drawingPage = drawingPage

        Try

            Dim colKeys As Collection = New Collection
            colKeys.Add(linkPortAKey)
            colKeys.Add(linkPortBKey)

            If blnExcelSource = False Then

                ' read the port records
                _clsLinkPort.dbSelectAttrPortDataByPortKeyCollection(colKeys)
                _clsData.loadAttrPortData(-1, Nothing, _clsLinkPort.m_ds.Tables(clsDC_RackData.DC_RackTables.tblAttrPortData.ToString))

                'read the network records
                _clsPortNet.dbSelectAttrNetworkByPortKeyCollection(colKeys)
                _clsData.loadAttrNetwork(-1, Nothing, _clsPortNet.m_ds.Tables(clsDC_RackData.DC_RackTables.tblAttrNetwork.ToString))

            End If

            Dim shpGroupA As Microsoft.Office.Interop.Visio.Shape = Nothing
            Dim colInputA As Collection = New Collection
            ' first the port information for the a side of the link
            shpGroupA = loadLinkPort(linkPortAKey, _clsLinkPort)
            If shpGroupA IsNot Nothing Then
                colInputA.Add(shpGroupA.ID)
                shpGroupA = Nothing
            End If
            ' then the network information for the a side of the linke (if there)
            shpGroupA = loadPortNet(linkPortAKey, _clsPortNet)
            If shpGroupA IsNot Nothing Then
                colInputA.Add(shpGroupA.ID)
                shpGroupA = Nothing
            End If

            ' the holding container for the side a port surrogates
            Dim shpContainerA As Microsoft.Office.Interop.Visio.Shape = Nothing
            shpContainerA = _clsVisio.addContainerToPage("Container 4", "contPortDataA", "PortDataA")

            ' create a list control
            Dim listControlA As Microsoft.Office.Interop.Visio.Shape = Nothing
            listControlA = _clsVisio.loadListControl(colInputA, _
                                              VisListAlignment.visListAlignLeftOrTop, _
                                              VisListDirection.visListDirTopToBottom)

            listControlA.ContainerProperties.LockMembership = True

            ' add the list control to the container
            _clsVisio.AddShapeToContainer(shpContainerA, listControlA)


            '*********************************************************************************************************************

            Dim shpGroupB As Microsoft.Office.Interop.Visio.Shape = Nothing
            Dim colInputB As Collection = New Collection
            ' first the port information for the a side of the link
            shpGroupB = loadLinkPort(linkPortBKey, _clsLinkPort)
            If shpGroupB IsNot Nothing Then
                colInputB.Add(shpGroupB.ID)
                shpGroupB = Nothing
            End If
            ' then the network information for the a side of the linke (if there)
            shpGroupB = loadPortNet(linkPortBKey, _clsPortNet)
            If shpGroupB IsNot Nothing Then
                colInputB.Add(shpGroupB.ID)
                shpGroupB = Nothing
            End If

            ' the holding container for the side a port surrogates
            Dim shpContainerB As Microsoft.Office.Interop.Visio.Shape = Nothing
            shpContainerB = _clsVisio.addContainerToPage("Container 4", "contPortDataB", "PortDataB")

            ' create a list control
            Dim listControlB As Microsoft.Office.Interop.Visio.Shape = Nothing
            listControlB = _clsVisio.loadListControl(colInputB, _
                                              VisListAlignment.visListAlignLeftOrTop, _
                                              VisListDirection.visListDirTopToBottom)
            listControlB.ContainerProperties.LockMembership = True

            ' add the list control to the container
            _clsVisio.AddShapeToContainer(shpContainerB, listControlB)



        Catch com As COMException
            '********* visio snippet vb.net vis0056vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(com, "Com+ Policy")
            If (rethrow) Then
                Throw
            End If

        Catch ex As Exception
            '********* visio snippet vb.net vis0059vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        _clsLinkPort = Nothing
        _clsPortNet = Nothing


    End Sub

    '
    ' based on the port key, read the network record(s) from the data source
    '
    Private Function loadPortNet _
        (ByVal intPortKey As Int32, _
         ByVal _clsPortNet As clsAttrNetwork) _
        As Microsoft.Office.Interop.Visio.Shape


        Dim shpContainer As Microsoft.Office.Interop.Visio.Shape = Nothing

        Try

            Me.sblStatusTwo.Text = "Net Loaded"


            _clsPortNet.drawingPage = drawingPage
            _clsPortNet._CustomStencilPath = _CustomStencilPath

            shpContainer = _clsPortNet.buildNetworkDataContainer("port", intPortKey, _clsData, vbTrue)


            Me.sblStatusTwo.Text = "Net Loaded"


        Catch com As COMException
            '********* visio snippet vb.net vis0056vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(com, "Com+ Policy")
            If (rethrow) Then
                Throw
            End If
        Catch sql As SqlException
            '********* visio snippet vb.net vis0057vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            '********* visio snippet vb.net vis0059vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If

        Finally



        End Try

        Return shpContainer

    End Function

    '
    ' based on the port key, read the port record(s) from the data source
    '
    Private Function loadLinkPort _
                (ByVal intPortKey As Int32, _
                 ByVal _clsLinkPort As clsAttrPortData) _
                As Microsoft.Office.Interop.Visio.Shape


        Dim shpContainer As Microsoft.Office.Interop.Visio.Shape = Nothing

        Try


            Me.sblStatusTwo.Text = "Port Started"

            _clsLinkPort.drawingPage = drawingPage
            _clsLinkPort._CustomStencilPath = _CustomStencilPath

            shpContainer = _clsLinkPort.buildPortDataContainer("port", intPortKey, _clsData, vbTrue)

            'Dim rdPortData As Microsoft.Office.Interop.Visio.DataRecordset = Nothing
            'rdPortData = _clsData._PortRd


            Me.sblStatusTwo.Text = "Port Loaded"


        Catch com As COMException
            '********* visio snippet vb.net vis0056vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(com, "Com+ Policy")
            If (rethrow) Then
                Throw
            End If
        Catch sql As SqlException
            '********* visio snippet vb.net vis0057vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            '********* visio snippet vb.net vis0059vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If

        Finally



        End Try

        Return shpContainer

    End Function

#End Region


#Region " Component Containers "

    '
    ' their needs to be two component containers (a-side and b-side). The component containers will hold
    ' two surrogate shapes, a component shape and an optional attrlocation shape, an optional component image
    ' These shapes are stored in a vertical oriented list control
    '
    Private Sub buildComponentContainers()


        Dim _clsComp As visRackLinkAttributes.clsComponents = New visRackLinkAttributes.clsComponents(drawingApplication, drawingDocument)
        _clsComp.drawingPage = drawingPage

        Dim _clsSubLoc As visRackLinkAttributes.clsAttrLocation = New visRackLinkAttributes.clsAttrLocation(drawingApplication, drawingDocument)
        _clsSubLoc.drawingPage = drawingPage

        Try

            Dim colKeys As Collection = New Collection

            ' first get the associated component records
            ' should only be two records (one for each side of link)
            Dim portQuery = From port In _clsData.m_ds.Tables(DC_RackTables.tblAttrPortData.ToString).AsEnumerable() _
                Select port

            Dim intPortKey As Int32 = -1
            Dim intCompKey As Int32 = -1
            For Each p In portQuery
                intCompKey = p.Item("portCompKey")
                intPortKey = p.Item("portKey")
                If intPortKey = linkPortAKey Then
                    portCompAKey = intCompKey
                End If
                If intPortKey = linkPortBKey Then
                    portCompBKey = intCompKey
                End If
                colKeys.Add(intCompKey)
            Next p


            Dim intLocACompKey As Int32 = portCompAKey
            Dim intlocBCompKey As Int32 = portCompBKey
            If blnExcelSource = False Then

                ' read the component records
                _clsComp.dbSelectComponentByCompKeyCollection(colKeys)
                _clsData.loadComponents(-1, Nothing, _clsComp.m_ds.Tables(clsDC_RackData.DC_RackTables.tblComponents.ToString))

                ' then get the root attrlocation record (this plays against the components location view)
                _clsSubLoc.dbSelectAttrLocationByComponentCollection(colKeys)
                _clsData.loadSubLocationTable(-1, _clsSubLoc.m_dt)


                ' read from the class datastore since we have an extra field in it to correlate to the components
                ' remember that this is going for the root components location (in most cases this is the rack)
                ' the actual (sub)component location would be a -1.........
                Dim sublocQuery = From subloc In _clsData.m_ds.Tables(DC_RackTables.tblAttrLocation.ToString).AsEnumerable() _
                        Select subloc
                Dim intLocKey As Int32 = -1

                For Each s In sublocQuery
                    intCompKey = s.Item("vwlocCompKey")
                    intLocKey = s.Item("attrlocKey")
                    If intCompKey = portCompAKey Then
                        subLocAKey = intLocKey
                    End If
                    If intCompKey = portCompBKey Then
                        subLocBKey = intLocKey
                    End If
                Next s

            Else
                ' dummy up the locations
                subLocAKey = 1
                subLocBKey = 1
                intLocACompKey = -1
                intlocBCompKey = -1
            End If



            ' the data has been gathered, now build the two containers
            Dim colInputA As Collection = New Collection
            Dim shpGroupA As Microsoft.Office.Interop.Visio.Shape = Nothing
            ' first the component information for the a side of the link
            shpGroupA = loadCompData(portCompAKey, _clsComp)
            If shpGroupA IsNot Nothing Then
                colInputA.Add(shpGroupA.ID)
                shpGroupA = Nothing
            End If

            ' then the sublocation information for the a side of the link (if there)
            shpGroupA = loadSubLocData(subLocAKey, _clsSubLoc, intLocACompKey)
            If shpGroupA IsNot Nothing Then
                colInputA.Add(shpGroupA.ID)
                shpGroupA = Nothing
            End If

            ' the holding container for the side a component surrogates
            Dim shpContainerA As Microsoft.Office.Interop.Visio.Shape = Nothing
            shpContainerA = _clsVisio.addContainerToPage("Container 4", "contCompDataA", "ComponentA")

            ' create a list control
            Dim listControlA As Microsoft.Office.Interop.Visio.Shape = Nothing
            listControlA = _clsVisio.loadListControl(colInputA, _
                                              VisListAlignment.visListAlignLeftOrTop, _
                                              VisListDirection.visListDirTopToBottom)

            listControlA.ContainerProperties.LockMembership = True

            ' add the list control to the container
            _clsVisio.AddShapeToContainer(shpContainerA, listControlA)


            ' Now the B side
            Dim colInputB As Collection = New Collection
            Dim shpGroupB As Microsoft.Office.Interop.Visio.Shape = Nothing
            ' first the component information for the a side of the link
            shpGroupB = loadCompData(portCompBKey, _clsComp)
            If shpGroupB IsNot Nothing Then
                colInputB.Add(shpGroupB.ID)
                shpGroupB = Nothing
            End If
            ' then the sublocation information for the a side of the linke (if there)
            shpGroupB = loadSubLocData(subLocBKey, _clsSubLoc, intlocBCompKey)
            If shpGroupB IsNot Nothing Then
                colInputB.Add(shpGroupB.ID)
                shpGroupB = Nothing
            End If

            ' the holding container for the side a component surrogates
            Dim shpContainerB As Microsoft.Office.Interop.Visio.Shape = Nothing
            shpContainerB = _clsVisio.addContainerToPage("Container 4", "contCompDataB", "ComponentB")

            ' create a list control
            Dim listControlB As Microsoft.Office.Interop.Visio.Shape = Nothing
            listControlB = _clsVisio.loadListControl(colInputB, _
                                              VisListAlignment.visListAlignLeftOrTop, _
                                              VisListDirection.visListDirTopToBottom)

            listControlB.ContainerProperties.LockMembership = True

            ' add the list control to the container
            _clsVisio.AddShapeToContainer(shpContainerB, listControlB)


        Catch com As COMException
            '********* visio snippet vb.net vis0056vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(com, "Com+ Policy")
            If (rethrow) Then
                Throw
            End If

        Catch ex As Exception
            '********* visio snippet vb.net vis0059vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        _clsComp = Nothing
        _clsSubLoc = Nothing


    End Sub

    '
    ' based on the port key, read the network record(s) from the data source
    '
    Private Function loadCompData _
        (ByVal intCompKey As Int32, _
         ByVal _clsCompData As clsComponents) _
        As Microsoft.Office.Interop.Visio.Shape


        Dim shpContainer As Microsoft.Office.Interop.Visio.Shape = Nothing

        Try

            Me.sblStatusTwo.Text = "Component Loading"


            _clsCompData.drawingPage = drawingPage
            _clsCompData._CustomStencilPath = _CustomStencilPath

            'If blnExcelSource = False Then
            '    _clsCompData.dbSelectComponentByCompKey(intCompKey)
            '    _clsData.loadComponents(-1, Nothing, _clsCompData.m_ds.Tables(DC_RackTables.tblComponents.ToString))
            'End If

            shpContainer = _clsCompData.buildComponentContainer(intCompKey, _clsData)

            Me.sblStatusTwo.Text = "Component Loaded"


        Catch com As COMException
            '********* visio snippet vb.net vis0056vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(com, "Com+ Policy")
            If (rethrow) Then
                Throw
            End If
        Catch sql As SqlException
            '********* visio snippet vb.net vis0057vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            '********* visio snippet vb.net vis0059vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If

        Finally



        End Try

        Return shpContainer

    End Function

    '
    ' based on the port key, read the port record(s) from the data source
    '
    Private Function loadSubLocData _
                (ByVal intLocKey As Int32, _
                 ByVal _clsSubLoc As clsAttrLocation, _
                 Optional ByVal intCompKey As Int32 = -1) _
                As Microsoft.Office.Interop.Visio.Shape


        Dim shpContainer As Microsoft.Office.Interop.Visio.Shape = Nothing

        Try


            Me.sblStatusTwo.Text = "subLocation Started"

            _clsSubLoc.drawingPage = drawingPage
            _clsSubLoc._CustomStencilPath = _CustomStencilPath

            shpContainer = _clsSubLoc.buildAttrLocationContainer(intLocKey, _clsData, intCompKey)

            Me.sblStatusTwo.Text = "subLocation Loaded"


        Catch com As COMException
            '********* visio snippet vb.net vis0056vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(com, "Com+ Policy")
            If (rethrow) Then
                Throw
            End If
        Catch sql As SqlException
            '********* visio snippet vb.net vis0057vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            '********* visio snippet vb.net vis0059vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If

        Finally



        End Try

        Return shpContainer

    End Function



#End Region


#End Region

#Region " External get/save "



    ' dictTables is a list of the tables we will use in this solution
    ' enumTable value and the exported table name
    Private dictTables As Dictionary(Of Int16, String)

    '
    ' used to load test case data from excel source
    '
    Private Sub loadExcelTestData()

        Try

            Me.sblStatusOne.Text = "Initializing"

            Dim dtTemp As System.Data.DataTable = Nothing
            Dim dictTemp As Dictionary(Of Int16, String) = New Dictionary(Of Int16, String)

            ' read the worksheet list into a local dictionary
            dictTemp = readExcelWorksheetList(My.Settings.path_custom_data & My.Settings.workbook_custom_data)
            Dim blnContains As Boolean = True
            ' are there enough worksheets
            If dictTables.Count <= dictTemp.Count Then
                For Each kvp In dictTables
                    If dictTemp.ContainsValue(kvp.Value) = False Then
                        blnContains = False
                    End If
                Next kvp
            Else
                MsgBox("Not enough worksheets for this solutions")
                Exit Sub
            End If

            If blnContains = False Then
                MsgBox("Could not find all necessary worksheets")
                Exit Sub
            End If

            ' now read the worksheets into the tables
            ' the keyvalue pairs are the table index, and the modified table name
            For Each kvp In dictTables
                dtTemp = Nothing
                dtTemp = readExcelWorksheet(My.Settings.path_custom_data & My.Settings.workbook_custom_data, kvp.Value)
                If dtTemp Is Nothing Then GoTo NextKvp
                dtTemp.TableName = CType(kvp.Key, DC_RackTables).ToString

                Select Case kvp.Key
                    Case DC_RackTables.tblComponents
                        Me.sblStatusTwo.Text = DC_RackTables.tblComponents.ToString & " loading "
                        _clsData.loadComponents(-1, Nothing, dtTemp)
                    Case DC_RackTables.tblDGMasters
                        Me.sblStatusTwo.Text = DC_RackTables.tblDGMasters.ToString & " loading "
                        _clsData.loadDgMasterTable(dtTemp)
                    Case DC_RackTables.tblNetworkLink
                        Me.sblStatusTwo.Text = DC_RackTables.tblNetworkLink.ToString & " loading "
                        _clsData.loadNetworkLinksTable(-1, Nothing, dtTemp)
                    Case DC_RackTables.tblAttrLocation
                        Me.sblStatusTwo.Text = DC_RackTables.tblAttrLocation.ToString & " loading "
                        _clsData.loadSubLocationTable(-1, dtTemp)
                    Case DC_RackTables.tblAttrPortData
                        Me.sblStatusTwo.Text = DC_RackTables.tblAttrPortData.ToString & " loading "
                        _clsData.loadAttrPortData(-1, Nothing, dtTemp)
                    Case DC_RackTables.tblAttrNetwork
                        Me.sblStatusTwo.Text = DC_RackTables.tblAttrNetwork.ToString & " loading "
                        _clsData.loadAttrNetwork(-1, Nothing, dtTemp)
                End Select
NextKvp:
            Next kvp

            Me.sblStatusOne.Text = "DataLoaded"
            Me.sblStatusTwo.Text = _clsData.m_ds.Tables.Count.ToString & " tables loaded"

        Catch ex As Exception
            '********* visio snippet vb.net vis0059vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    ' create the list of tables that we will need for this visio report
    ' this is pretty much all of the defined attributes for a rack mounted
    ' component
    '
    Private Sub loadTestDictTables()


        dictTables = Nothing
        dictTables = New Dictionary(Of Int16, String)

        ' the basics

        dictTables.Add(DC_RackTables.tblDGMasters, "dbo_tblDGMasters")
        dictTables.Add(DC_RackTables.tblNetworkLink, "dbo_tblNetworkLink")
        ' initial network data
        dictTables.Add(DC_RackTables.tblAttrNetwork, "dbo_tblAttrNetwork")
        dictTables.Add(DC_RackTables.tblAttrPortData, "dbo_tblAttrPortData")

        dictTables.Add(DC_RackTables.tblComponents, "dbo_tblComponents")
        dictTables.Add(DC_RackTables.tblAttrLocation, "dbo_tblAttrLocation")

    End Sub



    '
    ' Reads a workbook and returns a collection of worksheet names as a 
    ' dictionary
    '
    Private Function readExcelWorksheetList _
        (ByVal strWorkbook As String) _
        As Dictionary(Of Int16, String)

        Dim dictReturn As Dictionary(Of Int16, String) = New Dictionary(Of Int16, String)
        Dim objExcel As Microsoft.Office.Interop.Excel.Application = Nothing
        Dim objWorkBook As Microsoft.Office.Interop.Excel.Workbook = Nothing
        Dim objWorkSheets As Microsoft.Office.Interop.Excel.Worksheet = Nothing

        Try

            If (strWorkbook Is Nothing) Then
                Throw New System.ArgumentNullException("strWorkbookValueIsNull", _
                    "Workbook is Null.")
                GoTo ExitFunction
            End If

            If File.Exists(strWorkbook) = False Then
                MsgBox("Workbook (" & strWorkbook & ") not found")
                GoTo ExitFunction
            End If

            Dim ExcelSheetName As String = ""
            objExcel = CreateObject("Excel.Application")
            objWorkBook = objExcel.Workbooks.Open(strWorkbook)
            Dim intX As Integer = 1
            For Each objWorkSheets In objWorkBook.Worksheets
                dictReturn.Add(intX, objWorkSheets.Name)
                intX = intX + 1
            Next

ExitFunction:

        Catch com As COMException
            '********* visio snippet vb.net vis0056vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(com, "Com+ Policy")
            If (rethrow) Then
                Throw
            End If

        Catch ex As Exception
            '********* visio snippet vb.net vis0059vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If

        Finally

            objWorkSheets = Nothing
            objWorkBook = Nothing
            objExcel.Quit()
            objExcel = Nothing

        End Try

        Return dictReturn

    End Function

    '
    ' read a worksheet and return a datatable
    '
    Private Function readExcelWorksheet _
            (ByVal strWorkBook As String, _
            ByVal strWorkSheet As String) _
            As System.Data.DataTable


        Dim dtReturn As System.Data.DataTable = Nothing

        Try

            If (strWorkBook Is Nothing) _
                Or strWorkBook = "" Then
                Throw New System.ArgumentNullException("strWorkbookValueIsNull", _
                    "Workbook is Null.")
                GoTo ExitFunction
            End If

            If File.Exists(strWorkBook) = False Then
                MsgBox("Workbook (" & strWorkBook & ") not found")
                GoTo ExitFunction
            End If

            Dim DS As DataSet
            Dim MyCommand As OleDb.OleDbDataAdapter
            Dim MyConnection As OleDb.OleDbConnection
            Dim strConnection As String = "Provider=Microsoft.ACE.OLEDB.12.0;" _
                                          & "Data Source=" & strWorkBook & ";" _
                                          & "Extended Properties=" & """Excel 12.0 Xml;HDR=YES""" & ";"
            MyConnection = New OleDb.OleDbConnection(strConnection)
            MyCommand = New OleDb.OleDbDataAdapter( _
                         "select * from [" & strWorkSheet & "$]", MyConnection)
            DS = New System.Data.DataSet()
            MyCommand.Fill(DS)
            dtReturn = DS.Tables(0)
            dtReturn.TableName = strWorkSheet
            MyConnection.Close()

ExitFunction:

        Catch oledb As OleDbException
            '********* visio snippet vb.net visXXXX ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(oledb, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            '********* visio snippet vb.net visXXXX ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        Return dtReturn

    End Function

    '
    ' 
    '
    Private Sub saveVisioDocumentDialog()



        Try

            Dim visDoc As Microsoft.Office.Interop.Visio.Document = Me.drawingDocument

            ' flag the document as one of ours
            Dim docsheet As Microsoft.Office.Interop.Visio.Shape = visDoc.DocumentSheet
            _clsVisio.ForceUserProperty(docsheet, "Solution", "DC_Rack")
            _clsVisio.ForceUserProperty(docsheet, "Diagram", "LinkAttributes")

            SaveFileDialog.Filter = "vsd files (*.vsd)|*.vsd|All files (*.*)|*.*"
            SaveFileDialog.FilterIndex = 1
            SaveFileDialog.RestoreDirectory = True

            Dim dlgResult As DialogResult = SaveFileDialog.ShowDialog

            If dlgResult = DialogResult.OK Then
                If _clsData._BlnDR = vbTrue Then
                    Dim resultMsg As MsgBoxResult = MsgBox("Save Hidden Data?", MsgBoxStyle.YesNo, "SaveHiddenData")
                    If resultMsg = MsgBoxResult.No Then
                        ' get rid of recordsets, masters, and personal information
                        visDoc.RemoveHiddenInformation(Microsoft.Office.Interop.Visio.VisRemoveHiddenInfoItems.visRHIDataRecordsets + _
                                                       Microsoft.Office.Interop.Visio.VisRemoveHiddenInfoItems.visRHIMasters + _
                                                       Microsoft.Office.Interop.Visio.VisRemoveHiddenInfoItems.visRHIPersonalInfo)
                    Else

                        Dim resultMsgLink As MsgBoxResult = MsgBox("Try to link records before save?", MsgBoxStyle.YesNo, "Attempt to Link Records")
                        If resultMsgLink = MsgBoxResult.Yes Then
                            attemptLinkRecords()
                        End If
                    End If

                End If


                visDoc.SaveAsEx(SaveFileDialog.FileName, Microsoft.Office.Interop.Visio.tagVisOpenSaveArgs.visSaveAsWS + _
                                Microsoft.Office.Interop.Visio.tagVisOpenSaveArgs.visSaveAsListInMRU)

            Else

                MsgBox("Save Cancelled")

            End If

        Catch com As COMException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(com, "Com+ Policy")
            If (rethrow) Then
                Throw
            End If
        Catch err As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(err, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try


        System.GC.Collect()

    End Sub
    '
    '
    '
    Private Sub attemptLinkRecords()

        Try

            Dim visShapes As Microsoft.Office.Interop.Visio.Shapes = drawingPage.Shapes
            Dim visShape As Microsoft.Office.Interop.Visio.Shape
            Dim strShapeKey As String = ""
            Dim charSeparators() As Char = {"_"c}
            Dim count As Integer = 2
            Dim options As StringSplitOptions = StringSplitOptions.None
            Dim returnValue As String()
            Dim strTable As String = ""
            Dim strKey As String = ""
            Dim strFilter As String = ""
            Dim lngRowIds() As Int32
            Dim visRecordset As Microsoft.Office.Interop.Visio.DataRecordset = Nothing
            For Each visShape In visShapes
                strFilter = ""
                lngRowIds = {}
                strKey = ""
                visRecordset = Nothing
                ' was the attribute key saved
                If visShape.CellExists("user.attrkey", False) = True Then

                    strShapeKey = visShape.Cells("user.attrkey").ResultStr("")
                    returnValue = strShapeKey.Split(charSeparators, _
                            count, options)
                    strKey = LCase(returnValue(0))
                    Select Case strKey

                        Case "compkey"
                            strTable = clsDC_RackData.DC_RackTables.tblComponents.ToString
                            strFilter = "compKey ='" & CInt(returnValue(1)) & "'"
                            visRecordset = _clsData._CompRd
                            Exit Select
                        Case "linkkey"
                            strTable = clsDC_RackData.DC_RackTables.tblNetworkLink.ToString
                            strFilter = "linkKey ='" & CInt(returnValue(1)) & "'"
                            visRecordset = _clsData._LinkRd
                            Exit Select
                        Case "portkey"
                            strTable = clsDC_RackData.DC_RackTables.tblAttrPortData.ToString
                            strFilter = "portKey ='" & CInt(returnValue(1)) & "'"
                            visRecordset = _clsData._PortRd
                            Exit Select
                        Case "attrlockey"
                            strTable = clsDC_RackData.DC_RackTables.tblAttrLocation.ToString
                            If visShape.CellExists("prop.vwloccompkey", False) = False Then
                                strFilter = "attrlocKey ='" & CInt(returnValue(1)) & "'"
                            Else
                                strFilter = "vwlocCompKey = '" & CInt(visShape.Cells("prop.vwloccompkey").ResultIU) & "'"
                            End If
                            visRecordset = _clsData._SubLocationRd
                            Exit Select
                        Case "attrnetkey"
                            strTable = clsDC_RackData.DC_RackTables.tblAttrNetwork.ToString
                            strFilter = "attrnetKey ='" & CInt(returnValue(1)) & "'"
                            visRecordset = _clsData._AttrNetRd
                            Exit Select
                        Case Else
                            GoTo NextShape

                    End Select

                    If visRecordset IsNot Nothing Then
                        lngRowIds = visRecordset.GetDataRowIDs(strFilter)
                        If lngRowIds.Count = 1 Then
                            visShape.LinkToData(visRecordset.ID, lngRowIds(0), vbFalse)
                        End If ' test to make sure we found the record with the key
                    End If ' make sure it is not an empty recordset

                End If ' test to ensure it has an attribute key
NextShape:
            Next ' next shape on the page


        Catch com As COMException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(com, "Com+ Policy")
            If (rethrow) Then
                Throw
            End If
        Catch err As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(err, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Private Sub openVisioDocumentDialog()



        Try

            OpenFileDialog.InitialDirectory = "c:\"
            OpenFileDialog.FileName = "*.vsd"
            OpenFileDialog.Filter = "visio drawings (*.vsd)|*.vsd|All files (*.*)|*.*"
            OpenFileDialog.FilterIndex = 1
            OpenFileDialog.RestoreDirectory = True

            Dim strPath As String = ""
            Dim dlgResult As System.Windows.Forms.DialogResult = OpenFileDialog.ShowDialog

            If dlgResult = System.Windows.Forms.DialogResult.OK Then

                Me.drawingControl.Src = OpenFileDialog.FileName

                Me.drawingWindow = Me.drawingControl.Window
                Me.drawingApplication = Me.drawingControl.Window.Application
                Me.drawingDocument = Me.drawingApplication.ActiveDocument
                Me.drawingPage = Me.drawingApplication.ActivePage

                ' flag it as already having been saved
                Me.drawingDocument.Saved = True

            End If

        Catch com As COMException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(com, "Com+ Policy")
            If (rethrow) Then
                Throw
            End If
        Catch err As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(err, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub





#End Region

#Region " Methods "

    '
    ' considered colorbyvalue but did not get results we wanted (besides it was dropped in v2010)
    '
    Private Sub AddNetworkLayersToPage _
            (ByVal Page As Visio.Page)

        Dim pagCell As Visio.Cell
        Dim layersObj As Visio.Layers
        Dim layerObj As Visio.Layer

        Try


            ' Add network status layers
            layersObj = Page.Layers

            layerObj = layersObj.Add(dc_Network.netStatus.netAvailable.ToString)
            pagCell = Page.PageSheet.Cells("layers.color[1]")
            _clsVisio.SetCellValueToString(pagCell, VisDefaultColors.visGreen)

            layerObj = layersObj.Add(dc_Network.netStatus.netUnavailable.ToString)
            pagCell = Page.PageSheet.Cells("layers.color[2]")
            _clsVisio.SetCellValueToString(pagCell, VisDefaultColors.visRed)

            layerObj = layersObj.Add(dc_Network.netStatus.netErrors.ToString)
            pagCell = Page.PageSheet.Cells("layers.color[3]")
            _clsVisio.SetCellValueToString(pagCell, VisDefaultColors.visYellow)

            layerObj = layersObj.Add(dc_Network.netStatus.netUndefined.ToString)
            pagCell = Page.PageSheet.Cells("layers.color[4]")
            _clsVisio.SetCellValueToString(pagCell, VisDefaultColors.visBlue)


            ' add cable layers
            'unTerminated cable (we have one side but not the other)
            layerObj = layersObj.Add(dc_Link.linkRoute.lr_Unterminated.ToString)
            pagCell = Page.PageSheet.Cells("layers.color[5]")
            pagCell.Formula = 255
            ' inside a rack
            layerObj = layersObj.Add(dc_Link.linkRoute.lr_intraRack.ToString)
            pagCell = Page.PageSheet.Cells("layers.color[5]")
            pagCell.Formula = 255
            'between racks
            layerObj = layersObj.Add(dc_Link.linkRoute.lr_interRack.ToString)
            pagCell = Page.PageSheet.Cells("layers.color[5]")
            pagCell.Formula = 255
            'between sites
            layerObj = layersObj.Add(dc_Link.linkRoute.lr_Wan.ToString)
            pagCell = Page.PageSheet.Cells("layers.color[5]")
            pagCell.Formula = 255

        Catch com As COMException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(com, "Com+ Policy")
            If (rethrow) Then
                Throw
            End If
        Catch err As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(err, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Finally


        End Try


    End Sub


    '
    '
    '
    Private Function PutOnLegend() As Microsoft.Office.Interop.Visio.Shape

        Dim vsoShape As Microsoft.Office.Interop.Visio.Shape = Nothing

        Try

            Dim docStencil As Microsoft.Office.Interop.Visio.Document
            Dim strStencilName As String = "periph_u.vss"
            Dim strMaster As String = "Legend"

            Dim vsoPage As Microsoft.Office.Interop.Visio.Page = drawingPage

            vsoShape = _clsVisio.DropMasterOnPage(vsoPage, strMaster, strStencilName, 24, 24, VisUnitCodes.visInches)
            vsoShape.Name = "visLegend"
            vsoShape.NameU = "visLegend"
            vsoShape.Cells("PinX").Formula = "1in"
            vsoShape.Cells("PinY").Formula = "2in"

            ' since it has to be open, close it
            docStencil = drawingApplication.Documents(strStencilName)
            docStencil.Close()
            docStencil = Nothing

        Catch com As COMException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(com, "Com+ Policy")
            If (rethrow) Then
                Throw
            End If
        Catch err As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(err, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If

        End Try

        Return vsoShape

    End Function




#End Region


End Class